/******************************************************************************
 * 	Freeais.org
 * 	http://www.freeais.org		info@freeais.org
 * 	
 *  Copyright (c) 2007 
 *  
 * 		ynnor systems GmbH
 * 		Mundsburger Damm 45
 * 		22087 Hamburg
 * 		Germany
 * 
 * 		Alexander Lotter	lotter@ynnor.de
 * 		David Schmitz		schmitz@ynnor.de
 * 
 *	This file is part of Freeais.org.
 *
 *  Freeais.org is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Freeais.org is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 ******************************************************************************/

package freeais.queue;

import java.util.Vector;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import freeais.ais.IAISMessage;

public class AISMessageQueue {

	/** Queue buffers the input of the producers */
	private BlockingQueue<IAISMessage> queue;

	/** known consumer */
	private AbstractAISConsumer consumer = null;

	/** known producers */
	private Vector<AbstractAISProducer> producers;

	/**
	 * This is the default constructor of AISMessageQueue.
	 */
	public AISMessageQueue() {

		this.queue = new LinkedBlockingQueue<IAISMessage>();
		this.producers = new Vector<AbstractAISProducer>();

	}

	/**
	 * This method registers one single consumer to the queue.
	 * 
	 * @param consumer
	 */
	public void addConsumer(AbstractAISConsumer consumer) {

		if (this.consumer == null) {
			this.consumer = consumer;
			new Thread(consumer).start();
			
		}
		
	}

	/**
	 * This method can add multiple producers to the queue.
	 * 
	 * @param producer	the producer to add
	 */
	public void addProducer(AbstractAISProducer producer) {

		boolean known = this.producers.contains(producer);
		if (!known) {
			this.producers.add(producer);
			new Thread(producer).start();
		}

	}
	
	/**
	 * This method removes an AISProducer.
	 * @param producer	the producer to remove
	 */
	public void removeProducer(AbstractAISProducer producer) {
		
		if(this.producers.contains(producer)) {
			this.producers.remove(producer);
			producer.stop();
		}
		
	}

	/**
	 * Getter method for the queue.
	 * @return	the queue
	 */
	public BlockingQueue<IAISMessage> getQueue() {

		return queue;
	}
}
